﻿package plugin.suishenshop.game
{
    import cmd.*;
    import com.core.*;
    import consts.*;
    import flash.events.*;
    import manager.*;
    import morn.core.components.*;
    import morn.core.handlers.*;
    import plugin.suishenshop.*;
    import uiresource.shop.*;
    import view.*;
    import view.mornExtends.*;

    public class SuiShenShopUICtrl extends NewshopUI
    {
        private var _close:Function;
        private var cfgSSShop:Array;
        private var cfgItem:Object;
        private var cfgShop:Object;
        private var curLevel:int = 0;
        private var listArr:Array;

        public function SuiShenShopUICtrl(param1:Function)
        {
            this.cfgSSShop = ConfigDictionary.data.suishenshop;
            this.cfgItem = ConfigDictionary.data.item;
            this.cfgShop = ConfigDictionary.data.shop;
            this._close = param1;
            return;
        }// end function

        override protected function initializeView() : void
        {
            super.initializeView();
            var _loc_1:* = Control.create(SuiShenShopControl, this);
            ModelProxy.addObserver("suishenshop", _loc_1);
            closeBtn.addEventListener(MouseEvent.CLICK, this.closeHandler);
            this.showList();
            return;
        }// end function

        public function showList() : void
        {
            var _loc_5:int = 0;
            var _loc_6:Array = null;
            var _loc_7:Object = null;
            var _loc_1:* = UserData.getUserData("level");
            var _loc_2:Array = [];
            var _loc_3:int = 1;
            while (_loc_3 < this.cfgSSShop.length)
            {
                
                _loc_2[this.cfgSSShop[_loc_3].level] = int(this.cfgSSShop[_loc_3].level);
                _loc_3++;
            }
            var _loc_4:Array = [];
            for each (_loc_5 in _loc_2)
            {
                
                _loc_4.push(_loc_5);
            }
            _loc_6 = _loc_4.sort(Array.NUMERIC);
            if (_loc_1 >= _loc_6[0] && _loc_1 < _loc_6[1])
            {
                this.curLevel = _loc_6[0];
            }
            else if (_loc_1 >= _loc_4[1] && _loc_1 < _loc_6[2])
            {
                this.curLevel = _loc_6[1];
            }
            else if (_loc_1 >= _loc_6[2] && _loc_1 < _loc_6[3])
            {
                this.curLevel = _loc_6[2];
            }
            else if (_loc_1 >= _loc_6[3] && _loc_1 < _loc_6[4])
            {
                this.curLevel = _loc_6[3];
            }
            else
            {
                this.curLevel = _loc_6[4];
            }
            this.listArr = [];
            for each (_loc_7 in this.cfgSSShop)
            {
                
                if (int(_loc_7.level) == this.curLevel)
                {
                    this.listArr.push(_loc_7);
                }
            }
            shoplist.array = this.listArr;
            shoplist.renderHandler = new Handler(this.renderHandler);
            return;
        }// end function

        private function renderHandler(param1:Component, param2:int) : void
        {
            var _loc_3:Label = null;
            var _loc_4:FlashItem = null;
            var _loc_5:Object = null;
            var _loc_6:Object = null;
            var _loc_7:Label = null;
            var _loc_8:Label = null;
            var _loc_9:Label = null;
            var _loc_10:Button = null;
            var _loc_11:Object = null;
            if (param2 < shoplist.length)
            {
                _loc_3 = param1.getChildByName("name") as Label;
                _loc_3.text = this.cfgItem[int(shoplist.array[param2].itemid)].name;
                _loc_4 = param1.getChildByName("item") as FlashItem;
                _loc_5 = this.cfgItem[shoplist.array[param2].itemid];
                for each (_loc_6 in this.cfgShop)
                {
                    
                    if (_loc_6.itemid == shoplist.array[param2].itemid)
                    {
                        _loc_5.count = _loc_6.num;
                        break;
                    }
                }
                _loc_4.data = _loc_5;
                _loc_7 = param1.getChildByName("neirLabel") as Label;
                _loc_7.text = shoplist.array[param2].desc;
                _loc_8 = param1.getChildByName("lvLabel") as Label;
                _loc_8.text = "使用等级：" + this.cfgItem[int(shoplist.array[param2].itemid)].uselv;
                _loc_9 = param1.getChildByName("moneyLabel") as Label;
                _loc_9.text = shoplist.array[param2].price;
                _loc_10 = param1.getChildByName("goumaiBtn") as Button;
                _loc_10.clickHandler = new Handler(this.goumaiHandler, [param2]);
                if (param2 == 0)
                {
                    _loc_11 = UserData.getOnlineData("guide");
                    if (int(_loc_11[GuideType.GUIDE_BB1]) == 1 && int(_loc_11[GuideType.GUIDE_BB2]) == 0)
                    {
                        ModelProxy.change("guideitem", "guideid" + GuideType.GUIDE_BB2, _loc_10);
                        ModelProxy.change("guideitem", "guideid" + GuideType.GUIDE_BB3, _loc_10);
                        ModelProxy.change("guideitem", "guideid" + GuideType.GUIDE_BB5, _loc_10);
                        GuideView.getInstance().doGuide(GuideType.GUIDE_BB2);
                    }
                }
            }
            return;
        }// end function

        private function goumaiHandler(param1:int) : void
        {
            var _loc_3:Object = null;
            ModelProxy.change("guide", "hide", GuideType.GUIDE_BB2);
            ModelProxy.change("guide", "hide", GuideType.GUIDE_BB3);
            ModelProxy.change("guide", "hide", GuideType.GUIDE_BB5);
            var _loc_2:int = 0;
            for each (_loc_3 in this.cfgShop)
            {
                
                if (_loc_3.npcid == 0 && int(_loc_3.itemid) == this.listArr[param1].itemid)
                {
                    _loc_2 = int(_loc_3.id);
                    break;
                }
            }
            PluginManager.instance.sendToServer("shop", CMDShop.Bay, {id:_loc_2, count:1, auto:int(_loc_3.itemid)});
            return;
        }// end function

        private function closeHandler(event:MouseEvent) : void
        {
            this.close();
            return;
        }// end function

        private function close() : void
        {
            this._close();
            return;
        }// end function

    }
}
